package org.emf.plugins.tomcatlauncher.server;

/**
 * Kiah response.
 */
public class KyahResponse {

    /** Regex used as a pattern to define the response message format. */
    static final String RESPONSE_REGEX = "(.+?)[:?](.*?)/";

    /** Response token identifier. */
    private Token token;

    /** Response message. */
    private String message;

    /** Class constructor. */
    public KyahResponse(String token, String message) {
        this(Token.valueOf(Token.class, token), message);
    }

    /** Class constructor. */
    public KyahResponse(Token token, String message) {
        this.token = token;
        this.message = message;
    }

    /**
     * Gets the response token identifier.
     * @return the response token identifier.
     */
    public Token getToken() {
        return token;
    }

    /**
     * Gets the response message.
     * @return the response message.
     */
    public String getMessage() {
        return message;
    }

    /**
     * Gets the response as string.
     * @return the response as string.
     */
    public String getAsString() {
        return token + ":" + (message == null ? "" : message) + CommChannel.SLASH;
    }

    /**
     * If response is success.
     * @return TRUE if response is success, FALSE otehrwise.
     */
    public boolean isSuccess() {
        switch (token) {
        case STOP_OK:
            return true;
        case FAILURE:
        default:
            return false;
        }
    }

    /**
     * Response token identifier.
     */
    public enum Token {
        /** Indicates start command send successfully. */
        START_OK,
        /** Indicates stop command send successfully. */
        STOP_OK,
        /** Indicates connection was established.. */
        ESTABLISHED,
        /** Indicates failure in the command execution. */
        FAILURE;
    }

}
